//
//  LLMapInfoView.m
//  QMCXDriver
//
//  Created by Massimo on 2017/4/17.
//  Copyright © 2017年 Massimo. All rights reserved.
//

#import "BaoCheMapInfoView.h"

#define blockHeight  13.f
#define blockWhidth  48.f
#define lineColor

@interface BaoCheMapInfoView()
@property (nonatomic, weak) IBOutlet UIView *contentView;
@property (nonatomic, weak) IBOutlet UIView *headerView;
@property (nonatomic, assign)IBInspectable BOOL isOpen;
@property (nonatomic, strong)IBInspectable UIImage *openedImage;
@property (nonatomic, strong)IBInspectable UIImage *unOpenImage;
@end


@implementation BaoCheMapInfoView
@synthesize isOpen = _isOpen;

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)awakeFromNib{
    [super awakeFromNib];
    self.isOpen = NO;
    
}

- (void)openedLayoutContentView{
    CGFloat contentHeight = self.isOpen ? 250.f: 63.f;
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(contentHeight);
    }];
   
    [self setNeedsDisplay];
}



- (void)drawRect:(CGRect)rect {
    // Drawing code
    self.layer.masksToBounds = YES;
    UIImage *image = _isOpen ? self.openedImage : self.unOpenImage;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    CGFloat blockX = (CGRectGetWidth(rect) - blockWhidth)/2.0;
    CGFloat blockY = CGRectGetMaxY(rect) - 12.5;
    CGFloat radius = 3;
    
    //绘制底色
    
    CGContextSaveGState(context);
    
    [[UIColor whiteColor ] setFill];
    
    
    CGContextMoveToPoint(context, 0, 0);
     CGContextAddLineToPoint(context, 0, blockY - 2.0);
    CGContextAddLineToPoint(context, blockX, blockY - 2.0);
    CGContextAddArcToPoint(context, blockX, blockY + blockHeight - 2 , blockX+blockWhidth/2.0, blockY+blockHeight - 2, radius);
    CGContextAddArcToPoint(context, blockX + blockWhidth, blockY + blockHeight - 2 , blockX+blockWhidth, blockY, radius);
    CGContextAddLineToPoint(context, blockX + blockWhidth, blockY - 2.0);
    CGContextAddLineToPoint(context, CGRectGetWidth(rect), blockY - 2.0);
    CGContextAddLineToPoint(context, CGRectGetWidth(rect), 0);
    CGContextClosePath(context);
    CGContextFillPath(context);
    
    CGContextRestoreGState(context);
    
    //画底部的线条
    CGContextSetRGBStrokeColor(context,211.f/255.f, 211.f/255.f, 211.f/255.f, 1.0);
    CGContextMoveToPoint(context, 0, blockY);
    CGContextAddLineToPoint(context, blockX, blockY);
    CGContextAddArcToPoint(context, blockX, blockY + blockHeight - 2 , blockX+blockWhidth/2.0, blockY+blockHeight - 2, radius);
    CGContextAddArcToPoint(context, blockX + blockWhidth, blockY + blockHeight - 2 , blockX+blockWhidth, blockY, radius);
    CGContextAddLineToPoint(context, blockX + blockWhidth, blockY);
    CGContextAddLineToPoint(context, CGRectGetWidth(rect), blockY);
    CGContextStrokePath(context);
   
    //画箭头
    CGContextSaveGState(context);
    CGFloat arrowX = (CGRectGetWidth(rect) - image.size.width)/2.0;
    CGFloat arrowY = blockY  ;
    [image drawInRect:CGRectMake(arrowX, arrowY, image.size.width, image.size.height)];
    CGContextRestoreGState(context);
    CGContextFillPath(context);
    
    
    [self.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj != self.headerView) {
            obj.hidden = !self.isOpen;
        }
    }];
   
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.isOpen = !self.isOpen;
}



- (void)setIsOpen:(BOOL)isOpen{
    if (isOpen != _isOpen) {
       _isOpen = isOpen;
       
    }
    [self openedLayoutContentView];
}


@end
